package org.vfinal.sonidosl3tcraft;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Tonacho extends Fragment {
	private ReproducirSonido RS = new ReproducirSonido();
	static public MediaPlayer reproSonido = new MediaPlayer();
	private int codeAudioSonando = -1;

	public Tonacho() {
		// TODO Auto-generated constructor stub

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		Cursor fila = null;
		ArrayList<HashMap<String, String>> DatosAlex = new ArrayList<HashMap<String, String>>();

		BaseDeDatos b77 = new BaseDeDatos(getActivity().getApplicationContext());

		SQLiteDatabase db = b77.getReadableDatabase();

		// Si hemos abierto correctamente la base de datos
		if (db != null) {
			fila = db.rawQuery("select * from Frases WHERE autor=2", null);
			fila.moveToFirst();
			db.close();
			b77.close();
		}
		do {
			// if(fila.getInt(3)!=0){
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("titulo", fila.getString(1));
			map.put("subtitulo", fila.getString(2));
			DatosAlex.add(map);
			// }
		} while (fila.moveToNext());
		fila.close();

		final View v = LayoutInflater.from(getActivity()).inflate(
				R.layout.listview_tonacho, null);

		final ListView lv_tonacho = (ListView) v
				.findViewById(R.id.lv_Lista_Sonidos);

		SimpleAdapter lista_Alex = new SimpleAdapter(this.getActivity(),
				DatosAlex, R.layout.sonidotonacho_layout,
				new String[] { "titulo" }, new int[] { R.id.titulo });

		lv_tonacho.setAdapter(lista_Alex);
		lv_tonacho.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				Context contexto = getActivity().getApplicationContext();
				int resID = RS.getIdAudio(lv_tonacho, position, 2, contexto);
				reproducirSonido(resID, contexto);
			}
		});
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
	}

	public void reproducirSonido(int id, Context contexto) {
		if (reproSonido.isPlaying()) {
			if (codeAudioSonando == id) {
				reproSonido.stop();
				codeAudioSonando = -1;
			} else {
				codeAudioSonando = id;
				reproSonido.stop();
				Alexelcapo.reproSonido.stop();
				Chincheto77.reproSonido.stop();
				Otros.reproSonido.stop();
				reproSonido = new MediaPlayer();
				reproSonido = MediaPlayer.create(contexto, id);
				reproSonido.start();

			}
		} else {
			codeAudioSonando = id;
			Alexelcapo.reproSonido.stop();
			Chincheto77.reproSonido.stop();
			Otros.reproSonido.stop();
			reproSonido = new MediaPlayer();
			reproSonido = MediaPlayer.create(contexto, id);
			reproSonido.start();
		}
	}

}
